package com.cw2.behavioral.interpreter;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.HashSet;
import java.util.Set;

/**
 * 定义环境类
 */
public class Area {
    // 城市
    Set<String> city = new HashSet<String>();
    // 类型
    Set<String> type = new HashSet<String>();

    IDCardExpression idCardExpression; // 解释器

    public Area(){
        city.add("武汉市");
        city.add("上海市");

        type.add("医生");
        type.add("儿童");

        // 定义城市解析器
        TerminalExpression city = new TerminalExpression(this.city, ":");
        TerminalExpression type = new TerminalExpression(this.type, "-");

        idCardExpression = new OrExpression(city, type);
    }

    /**
     * 判断是否为免费人群
     * @param expression
     */
    void getTicket(String expression){
        boolean interpreter = idCardExpression.interpreter(expression);
        if (interpreter){
            System.out.println("恭喜你免费通过");
        }else {
            System.out.println("对不起，请购票");
        }
    }
}
